package com.geekchique.runningbuddy.activity;

import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.IntentReceiver;
import android.content.ServiceConnection;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.Handler;
import android.view.KeyEvent;

import com.geekchique.runningbuddy.R;
import com.geekchique.runningbuddy.controller.CurrentRunMapControllerImpl;
import com.geekchique.runningbuddy.controller.ICurrentRunMapController;
import com.geekchique.runningbuddy.provider.ActivityID;
import com.geekchique.runningbuddy.service.IRemoteStopClockService;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;

public class CurrentRunMap extends MapActivity {

	private ICurrentRunMapController mapOfJogController;
	public LocationManager locationManager = null;
	protected MyIntentReceiver myIntentReceiver = null;
	final Handler mHandler = new Handler();

  @Override
  public void onCreate(Bundle icicle) {
      super.onCreate(icicle);
      this.setContentView(R.layout.current_run_map_view);
      this.myIntentReceiver = new MyIntentReceiver();

      mapOfJogController = new CurrentRunMapControllerImpl(this);

      mapOfJogController.createMapOfJogView((MapView)findViewById(R.id.map));

      mapOfJogController.startTrackingRoute();

  	}

	public boolean onKeyDown(int keyCode, KeyEvent event) {
		return mapOfJogController.onKeyDown(keyCode, event);
	}

	public void startStopwatchService(ServiceConnection connection) {
		this.registerReceiver(this.myIntentReceiver, new IntentFilter(ActivityID.MY_LOCATION_CHANGED_ACTION));
        bindService(new Intent(IRemoteStopClockService.class.getName()), connection, Context.BIND_AUTO_CREATE);
	}

	public void stopStopwatchService(ServiceConnection connection) {
		unbindService(connection);
		unregisterReceiver(this.myIntentReceiver);
	}


	@Override
	protected void onFreeze(Bundle outState) {
		super.onFreeze(outState);
		unregisterReceiver(this.myIntentReceiver);
	}

	class MyIntentReceiver extends IntentReceiver {

		@Override
		public void onReceiveIntent(Context context, Intent intent) {
			mapOfJogController.updateRunningPath();
		}
	}



}